package com.neuedu.cockpit.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.cockpit.domain.HmCarInfo;
import com.neuedu.cockpit.domain.HmCarState;
import com.neuedu.cockpit.domain.HmControlLog;
import com.neuedu.cockpit.mapper.HmCarStateMapper;
import com.neuedu.cockpit.mapper.HmControlLogMapper;
import com.neuedu.cockpit.service.IHmCarInfoService;
import com.neuedu.cockpit.service.IHmCarStateService;
import com.neuedu.common.constant.Command;
import com.neuedu.common.domain.po.CommandPo;

import com.neuedu.common.service.WebSocketServer;
import com.neuedu.common.utils.SecurityUtils;
import com.neuedu.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 车辆状态Service业务层处理
 *
 * @author neuedu
 * @date 2024-04-29
 */
@Service
public class HmCarStateServiceImpl extends ServiceImpl<HmCarStateMapper, HmCarState> implements IHmCarStateService {
    @Autowired
    private HmCarStateMapper hmCarStateMapper;

    @Autowired
    private HmControlLogMapper hmControlLogMapper;

    @Autowired
    private IHmCarInfoService hmCarInfoService;



    @Autowired
    private WebSocketServer webSocketServer;

    // MQTT配置已移除

    /**
     * 查询车辆状态
     *
     * @param carId 车辆状态主键
     * @return 车辆状态
     */
    @Override
    public HmCarState selectHmCarStateByCarId(Long carId) {
        return hmCarStateMapper.selectHmCarStateByCarId(carId);
    }

    /**
     * 查询车辆状态列表
     *
     * @param hmCarState 车辆状态
     * @return 车辆状态
     */
    @Override
    public List<HmCarState> selectHmCarStateList(HmCarState hmCarState) {
        return hmCarStateMapper.selectHmCarStateList(hmCarState);
    }

    /**
     * 新增车辆状态
     *
     * @param hmCarState 车辆状态
     * @return 结果
     */
    @Override
    public int insertHmCarState(HmCarState hmCarState) {
        return hmCarStateMapper.insertHmCarState(hmCarState);
    }

    /**
     * 修改车辆状态
     *
     * @param hmCarState 车辆状态
     * @return 结果
     */
    @Override
    public int updateHmCarState(HmCarState hmCarState) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        String userName = SecurityUtils.getLoginUser().getUsername();
        HmCarInfo hmCarInfo;
        if (hmCarState.getCarId() == null) {
            hmCarInfo = hmCarInfoService.selectHmCarInfoByUserIdAndCarVin(userId, hmCarState.getCarVin());
            if (hmCarInfo == null) {
                return -1;
            }
        } else {
            hmCarInfo = hmCarInfoService.getById(hmCarState.getCarId());
        }
        // 电池电量
        if (hmCarState.getBatteryPower() != null) {
            hmCarInfo.setBatteryPower(hmCarState.getBatteryPower());
            hmCarInfoService.updateHmCarInfo(hmCarInfo);
        }
        int result;
        HmControlLog hmControlLog = new HmControlLog();
        hmCarState.setCarId(hmCarInfo.getId());
        HmCarState state = hmCarStateMapper.selectHmCarStateByCarId(hmCarInfo.getId());
        if (state == null) {
            result = hmCarStateMapper.insertHmCarState(getHmCarState(hmCarState, hmControlLog));
        } else {
            result = hmCarStateMapper.updateHmCarState(getHmCarState(hmCarState, hmControlLog));
        }
        HmCarState now = hmCarStateMapper.selectHmCarStateByCarId(hmCarState.getCarId());
        // 风扇状态判断
        CommandPo commandPo = new CommandPo();
        commandPo.setCarId(hmCarState.getCarId());
        if (StringUtils.equals(now.getFanState(), "1")) {
            commandPo.setCommand(Command.OPENFAN);
            // MQTT发送已移除
        } else {
            commandPo.setCommand(Command.CLOSEFAN);
            // MQTT发送已移除
        }

        // 电池电量
        now.setBatteryPower(hmCarInfo.getBatteryPower());
        // 发送MQTT - 已移除
        // 发送websocket
        webSocketServer.broadcast(JSON.toJSONString(now));
        if (StringUtils.isNotBlank(hmControlLog.getOperaType())) {
            hmControlLog.setUserId(userId);
            hmControlLog.setCarId(hmCarInfo.getId());
            hmControlLog.setOperaContent("用户" + userName + "控制车辆" + hmCarInfo.getCarName() + "进行了" + hmControlLog.getOperaType() + "操作");
            hmControlLog.setCreateTime(new Date());
            hmControlLogMapper.insertHmControlLog(hmControlLog);
        }
        return result;
    }

    /**
     * 批量删除车辆状态
     *
     * @param carIds 需要删除的车辆状态主键
     * @return 结果
     */
    @Override
    public int deleteHmCarStateByCarIds(Long[] carIds) {
        return hmCarStateMapper.deleteHmCarStateByCarIds(carIds);
    }

    /**
     * 删除车辆状态信息
     *
     * @param carId 车辆状态主键
     * @return 结果
     */
    @Override
    public int deleteHmCarStateByCarId(Long carId) {
        return hmCarStateMapper.deleteHmCarStateByCarId(carId);
    }

    private HmCarState getHmCarState(HmCarState hmCarState, HmControlLog hmControlLog) {
        // 除雾
        if (StringUtils.isNotBlank(hmCarState.getClearFog())) {
            HmCarState state = hmCarStateMapper.selectHmCarStateByCarId(hmCarState.getCarId());
            // 开除雾
            if (StringUtils.equals(hmCarState.getClearFog(), "1")) {
                hmControlLog.setOperaType("打开除雾");
                hmCarState.setFanState("1");
            } else {
                hmControlLog.setOperaType("关闭除雾");
                if (StringUtils.equals(state.getAuto(), "1")) {
                    hmCarState.setFanState("1");
                } else {
                    hmCarState.setFanState("0");
                }
            }
        } else if (StringUtils.isNotBlank(hmCarState.getAuto())) {
            HmCarState state = hmCarStateMapper.selectHmCarStateByCarId(hmCarState.getCarId());
            if (StringUtils.equals(hmCarState.getAuto(), "1")) {
                hmCarState.setWindLevel("4");
                hmCarState.setSetTemp("3");
                hmCarState.setAcBtn("1");
                hmCarState.setFanState("1");
                hmControlLog.setOperaType("打开空调");
            } else {
                hmCarState.setAcBtn("0");
                hmControlLog.setOperaType("关闭空调");
                if (StringUtils.equals(state.getClearFog(), "1")) {
                    hmCarState.setFanState("1");
                } else {
                    hmCarState.setFanState("0");
                }
            }
            // 门
        } else if (StringUtils.isNotBlank(hmCarState.getDoor())) {
            if (StringUtils.equals(hmCarState.getDoor(), "1")) {
                hmControlLog.setOperaType("打开车门");
            } else {
                hmControlLog.setOperaType("关闭车门");
            }
            // 灯
        } else if (StringUtils.isNotBlank(hmCarState.getLight())) {
            if (StringUtils.equals(hmCarState.getLight(), "1")) {
                hmControlLog.setOperaType("打开车灯");
            } else {
                hmControlLog.setOperaType("关闭车灯");
            }
            // 风速
        } else if (StringUtils.isNotBlank(hmCarState.getWindLevel())) {
            hmControlLog.setOperaType("调节风速挡位");
            // 内外循环
        } else if (StringUtils.isNotBlank(hmCarState.getCirculate())) {
            if (StringUtils.equals(hmCarState.getCirculate(), "1")) {
                hmControlLog.setOperaType("打开内循环");
            } else {
                hmControlLog.setOperaType("打开外循环");
            }
            // 设置温度
        } else if (StringUtils.isNotBlank(hmCarState.getSetTemp())) {
            // 设置温度
            hmControlLog.setOperaType("设置温度");
            // A/C
        } else if (StringUtils.isNotBlank(hmCarState.getAcBtn())) {
            if (StringUtils.equals(hmCarState.getAcBtn(), "1")) {
                hmControlLog.setOperaType("打开A/C");
            } else {
                hmControlLog.setOperaType("关闭A/C");
            }
        }
        return hmCarState;
    }

}
